﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace SMS.QuanLyTinNhan
{
    public partial class fThongBaoSoTinNhanCanGui : Form
    {
        fLogin main;
        
        public fThongBaoSoTinNhanCanGui(fLogin main1)
        {
            InitializeComponent();
            main = main1;
        }

        private void bntHuy_Click(object sender, EventArgs e)
        {
            this.Visible = false;
            main.HeThong.Visible = true;
        }

        public void fThongBaoSoTinNhanCanGui_Load(object sender, EventArgs e)
        {

            try
            {
                #region Display all available COM Ports
                string[] ports = SerialPort.GetPortNames();

                // Add all port names to the combo box:
                this.cboPort.Items.Clear();
                foreach (string port in ports)
                {
                    this.cboPort.Items.Add(port);
                }
                this.cboPort.SelectedIndex = 0;
                #endregion

            }
            catch (Exception ex)
            {
                MessageBox.Show("Không tìm thấy thiết bị kết nối !");
            }
        }

        private void bntKetNoi_Click(object sender, EventArgs e)
        {
            try
            {
                //Open communication port 
                main.port = main.objclsSMS.OpenPort(this.cboPort.Text, 9600, 8, 10000, 10000);

                if (main.port != null)
                {
                    bntKetNoi.Visible = false;
                    bntNgatKetNoi.Visible = true;
                    bntNgatKetNoi.Enabled = true;
                    main.HeThong.picConnect.Visible = true;
                    main.HeThong.picDicconnect.Visible = false;
                    bntSave.Enabled = true;
                    bntRefresh.Enabled = false;
                    cboPort.Enabled = false;
                }

                else
                {
                    MessageBox.Show("Kết nối thất bại !");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Kết nối thất bại !");
            }
        }

        private void NgatKetNoi()
        {
            timer1.Stop();
            main.objclsSMS.ClosePort(main.port);
            bntKetNoi.Visible = true;
            bntNgatKetNoi.Visible = false;
            main.HeThong.picConnect.Visible = false;
            main.HeThong.picDicconnect.Visible = true;
            bntSave.Enabled = false;
            bntRefresh.Enabled = true;
            cboPort.Enabled = true;

        }

        private void bntNgatKetNoi_Click(object sender, EventArgs e)
        {
            NgatKetNoi();
        }

        private void bntSave_Click(object sender, EventArgs e)
        {
           
            timer1.Enabled = true;
            timer1.Start();
            this.Visible = false;
            main.HeThong.Visible = true;
            bntSave.Enabled = false;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (backgroundWorker1.IsBusy)
            {
                
            }
            else
            {
                backgroundWorker1.RunWorkerAsync();
            }
            
            //backgroundWorker1.CancelAsync();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                #region Display all available COM Ports
                string[] ports = SerialPort.GetPortNames();

                // Add all port names to the combo box:
                this.cboPort.Items.Clear();
                foreach (string port in ports)
                {
                    this.cboPort.Items.Add(port);
                }
                this.cboPort.SelectedIndex = 0;
                #endregion

            }
            catch (Exception ex)
            {
                MessageBox.Show("Không tìm thấy thiết bị kết nối !");
            }
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            main.tinnhan.GuiTinNhan();
        }
    }
}
